const { Sequelize, Model, Op } = require("sequelize");
const { db } = require("../core/db");
const { Like } = require('./like');
const like = require("./like");


class HotBook extends Model {
  static async getData() {
    const books = await HotBook.findAll({
      order: ["index"],
    });

    let artIds = [];
    books.forEach((el) => {
      artIds.push(el.id);
    });
    const likeList = await Like.getLikeList(artIds, 400)

    // books.forEach(book => {
    //   likeList.forEach(like => {

    //   });
    // });

    books.forEach(book => {
      let count = 0
      likeList.forEach(like => {
        if(+book.id === +like.art_id){
          count = like.dataValues.count
        }
      })
      book.setDataValue('likeNums',count)
      return book
    });
    return books
  }
}
HotBook.init(
  {
    index: Sequelize.INTEGER,
    image: Sequelize.STRING,
    author: Sequelize.STRING,
    title: Sequelize.STRING,
  },
  { sequelize: db, tableName: "hot_book" }
);

module.exports = {
  HotBook,
};
